package org.example.server.tcp;

import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer;
import org.example.server.HttpServer;

/**
 * @description TODO
 * @author sats@jz
 * @date 2025年01月03日 19:46
 */
public class VertxTcpServer implements HttpServer {
    private byte[] handleRequest(byte[] requestData){
        return "TCP: Hello Client".getBytes();
    }
    @Override
    public void doStart(int port) {
        Vertx vertx = Vertx.vertx();

        // 创建TCP服务器
        NetServer netServer = vertx.createNetServer();
        // 处理请求

//        netServer.connectHandler(netSocket -> {
//
//            // 处理连接
//            netSocket.handler(buffer -> {
//                // 处理接受到的字节数组
//                byte[] bytes = buffer.getBytes();
//                byte[] responseData = handleRequest(bytes);
//                netSocket.write(Buffer.buffer(responseData));
//                System.out.println("Received request from client: " + buffer.toString());
//            });
//        });
        netServer.connectHandler(new TcpServerHandler());

        netServer.listen(port, netServerAsyncResult -> {
            if (netServerAsyncResult.succeeded()) {
                System.out.println("TCP服务器启动成功，监听端口：" + port);
            } else {
                System.out.println("TCP服务器启动失败：" + netServerAsyncResult.cause());
            }
        });
    }

    public static void main(String[] args) {
        new VertxTcpServer().doStart(8080);
    }

}


